package com.imti.mall.sale.mapper;

import com.imti.mall.model.sms.pojo.SmsHomeAdvertise;
import com.imti.mall.model.sms.pojo.SmsHomeNewProduct;
import org.apache.ibatis.jdbc.SQL;

public class SmsHomeAdvertiseMapper {

    /**
     * 查询首页轮播广告表
     */
    public String findSmsHomeAdvertise(final SmsHomeAdvertise smsHomeAdvertise){
        return new SQL(){
            {
                SELECT(" id,name,type,pic,start_time,end_time,status,click_count,order_count,url,note");
                FROM(" sms_home_advertise");
                StringBuffer sb=new StringBuffer();
                if(smsHomeAdvertise!=null){

                    if(smsHomeAdvertise.getPic()!=null){
                        sb.append(" and pic=#{pic}");
                    }

                    if(smsHomeAdvertise.getType()!=null){
                        sb.append(" and type=#{type}");
                    }

                    if(smsHomeAdvertise.getName()!=null){
                        sb.append(" and name LIKE concat('%',#{name},'%')");
                    }
                    if(smsHomeAdvertise.getStartTime()!=null){
                        sb.append(" and start_time=#{startTime}");
                    }
                    if(smsHomeAdvertise.getEndTime()!=null){
                        sb.append(" and end_time=#{endTime}");
                    }
                    if(smsHomeAdvertise.getStatus()!=null){
                        sb.append(" and status=#{status}");
                    }
                    if(smsHomeAdvertise.getClickCount()!=null){
                        sb.append(" and click_count=#{clickCount}");
                    }

                    if(smsHomeAdvertise.getOrderCount()!=null){
                        sb.append(" and order_count=#{orderCount}");
                    }

                    if(smsHomeAdvertise.getUrl()!=null){
                        sb.append(" and url=#{url}");
                    }

                    if(smsHomeAdvertise.getNote()!=null){
                        sb.append(" and note=#{note}");
                    }

                }
                WHERE(" 1=1 "+sb.toString());
                ORDER_BY(" sort desc");
            }
        }.toString();
    }


    /**
     * 修改首页轮播广告表
     * @param smsHomeAdvertise
     * @return
     */
    public String updateSmsHomeAdvertise(final SmsHomeAdvertise smsHomeAdvertise){
        return new SQL(){
            {
                UPDATE(" sms_home_advertise ");
                if(smsHomeAdvertise.getPic()!=null){
                    SET("  pic=#{pic}");
                }

                if(smsHomeAdvertise.getType()!=null){
                    SET("  type=#{type}");
                }

                if(smsHomeAdvertise.getName()!=null){
                    SET("  name=#{name}");
                }
                if(smsHomeAdvertise.getStartTime()!=null){
                    SET("  start_time=#{startTime}");
                }
                if(smsHomeAdvertise.getEndTime()!=null){
                    SET("  end_time=#{endTime}");
                }
                if(smsHomeAdvertise.getStatus()!=null){
                    SET("  status=#{status}");
                }
                if(smsHomeAdvertise.getClickCount()!=null){
                    SET("  click_count=#{clickCount}");
                }

                if(smsHomeAdvertise.getOrderCount()!=null){
                    SET("  order_count=#{orderCount}");
                }

                if(smsHomeAdvertise.getUrl()!=null){
                    SET("  url=#{url}");
                }

                if(smsHomeAdvertise.getNote()!=null){
                    SET("  note=#{note}");
                }
                if(smsHomeAdvertise.getSort()!=null){
                    SET("  sort=#{sort}");
                }

                WHERE(" id=#{id} ");
            }
        }.toString();
    }

}
